//package com.lzx.adx.report.job;
//
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.util.URLUtil;
//import cn.hutool.http.HttpUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.lzs.adx.commmon.redis.RedisUtils;
//import com.lzs.adx.commmon.utils.Result;
//import com.lzx.adx.report.cache.AccountLocalCache;
//import com.lzs.adx.commmon.constant.RedisCacheKey;
//import com.lzx.adx.report.dto.AccountDTO;
//import com.lzx.adx.report.dto.ReportDTO;
//import com.lzx.adx.report.service.ReportService;
//import com.lzx.adx.report.util.AdPerUtil;
//import com.lzx.adx.report.util.URLEncoderDecoderTool;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.GetMapping;
//
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//
//@Component
//@EnableScheduling
//@Slf4j
//public class WangWeiDangDangJob {
//
//
//    @Autowired
//    private ReportService reportService;
//
//    @Autowired
//    private RedisUtils redisUtils;
//
//
//    @Scheduled(cron = "0 */10 * ? * *")
//    public void reportEvent() {
//        List<Object> list = redisUtils.getList(RedisCacheKey.qtt_oneway_callback);
//        if(CollectionUtil.isEmpty(list)){return;}
//        list.forEach(item -> {
//            Map<String,Object> params=new HashMap();
//            params.put("bizRequestId",item.toString());
//            this.alCallBackUrl(item.toString(),"3");
//        });
//    }
//
//
//
//    /**
//     * 万唯当当的回传
//     * @return ResponseEntity
//     * @throws Exception
//     */
//    @GetMapping("/callback")
//    public ResponseEntity alCallBackUrl(String requestId,String eventType) {
//        log.info("万维当当回调趣头条信息，请求流水号{},回调类型为{}", requestId, eventType);
//        if (StringUtils.isBlank(requestId)||StringUtils.isBlank(eventType)) {
//            log.info("万维当当回调趣头条信息参数异常");
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//        }
//        //1.把转换结果更新到数据库
//        Map<String, Object> params = new HashMap<>();
//        params.put("bizRequestId", requestId);
//        List<ReportDTO> list = reportService.list(params);
//        long currentTimeMillis = System.currentTimeMillis();
//        ReportDTO reportDTO;
//        if (CollectionUtil.isNotEmpty(list)) {
//            reportDTO = list.get(0);
//        } else {
//            log.error("当前流水号{}，系统当前不存在", requestId);
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//        }
//        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
//        String sspExtend = accountDTO.getSspExtend();
//        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
//        String transformType = jsonObject.getString(eventType);
//        if (StringUtils.isBlank(transformType)){
//            log.info("万维当当回调趣头条信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,eventType);
//            return ResponseEntity.ok(new Result());
//        }
//        reportDTO.setResultType(eventType);
//        reportDTO.setResultTime(String.valueOf(currentTimeMillis));
//        reportDTO.setUpdateTime(new Date());
//        reportService.updateData(reportDTO,eventType);
//        Double per = jsonObject.getDouble("per");
//        if (Objects.nonNull(per)) {
//            if(!AdPerUtil.randomPer(per)) {
//                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
//                return ResponseEntity.ok(new Result());
//            }
//        }
//        //3，获取回传地址，进行解码
//        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
//        //拼接完整的请求参数
//        Map<String, Object> paramsReq = new HashMap<>();
//        paramsReq.put("op2", transformType);
//        paramsReq.put("opt_active_time", currentTimeMillis / 1000);
//        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
//        log.info("请求流水号{}万维当当回调趣头条的完整地址为：{}", requestId, finalUrl);
//        String result = HttpUtil.get(finalUrl);
//        log.info("请求流水号{}万维当当回调趣头条的结果为：{}", requestId, result);
//        return ResponseEntity.ok(new Result());
//    }
//
//}
